<?php

/**
 * This is the model class for table "nhomlop".
 *
 * The followings are the available columns in table 'nhomlop':
 * @property string $maNhom
 * @property string $makhoilop
 * @property string $mamh
 * @property integer $soSV
 * @property integer $hocki
 * @property string $namhoc
 * @property string $ghichu
 */
class Nhomlop extends CActiveRecord
{
	/**
	 * @return string the associated database table name
	 */
	 
	public $chuongtrinh_khoaphutrach_search;
	public $chuongtrinh_khoaday_search;
	
	public function tableName()
	{
		return 'nhomlop';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('maNhom, makhoilop, mamh, soSV, hocki, namhoc', 'required'),
			array('soSV, hocki', 'numerical', 'integerOnly'=>true),
			array('maNhom, makhoilop, mamh, namhoc', 'length', 'max'=>128),
			array('ghichu', 'safe'),
			// The following rule is used by search().
			// @todo Please remove those attributes that should not be searched.
			array('maNhom, makhoilop, mamh, soSV, hocki, namhoc, ghichu, chuongtrinh.madonvi, chuongtrinh_khoaphutrach_search, chuongtrinh.madonviday, chuongtrinh_khoaday_search', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'monhoc' => array(self::BELONGS_TO, 'Monhoc','mamh'),
			'chuongtrinh' => array(self::BELONGS_TO, 'Chuongtrinh', array('mamh'=>'mamh'), 'through'=>'monhoc'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'maNhom' => ' Nhóm lớp',
			'makhoilop' => 'Khối lớp',
			'mamh' => 'Học phần',
			'soSV' => 'Số sinh viên',
			'hocki' => 'Học kì',
			'namhoc' => 'Năm học',
			'ghichu' => 'Ghi chú',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria=new CDbCriteria;
		$criteria->with=array('monhoc');
		$criteria->with=array('chuongtrinh');

		$criteria->compare('maNhom',$this->maNhom,true);
		$criteria->compare('makhoilop',$this->makhoilop,true);
		if ($this->mamh) 
		{
            if (strpos($this->mamh,',') !==false) {
                $cities = explode(',',$this->mamh);
                $criteria->addInCondition('mamh',$cities);
            } else {
                $criteria->compare('mamh',$this->mamh,true);
            }
        }
		$criteria->compare('soSV',$this->soSV);
		$criteria->compare('hocki',$this->hocki);
		$criteria->compare('namhoc',$this->namhoc,true);
		$criteria->compare('ghichu',$this->ghichu,true);
		$criteria->compare('chuongtrinh.madonvi',$this->chuongtrinh_khoaphutrach_search);
		$criteria->compare('chuongtrinh.madonviday',$this->chuongtrinh_khoaday_search);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Nhomlop the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}
}
